//
//  UserNameController.m
//  CoolTennisBall
//
//  Created by Coollang on 16/8/26.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "UserNameController.h"
#import "FlatUIKit.h"


// 签名字数
#define kUserSignNameMaxNum 15

@interface UserNameController ()<UITextFieldDelegate, UITextViewDelegate>

@property (nonatomic, strong) FUITextField *textField;


@property (nonatomic, strong) UITextView *textView;


@property (nonatomic, strong) UILabel *countLable;

@end

@implementation UserNameController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithHex:0xf3f3f3];
    
    if (self.userSignName != nil) {
        [self setUpUserSignNameUI];
    }else {
        [self setUpUserNameUI];
    }
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self configureNav];

}

- (void)configureNav{
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sport_icon_blackbackArrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save", @"保存") style:UIBarButtonItemStylePlain target:self action:@selector(saveChange)];
}


- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)saveChange {
    
    [self.navigationController popViewControllerAnimated:YES];
    
    if (self.userNameBlock) {
        if (self.textField.text.length <= 0) {
            self.userNameBlock(@"");
        }else {
            self.userNameBlock(self.textField.text);
        }
    }else if (self.userSignNameBlock) {
        if (!self.textView.hasText) {
            self.userSignNameBlock(@"");
        }else {
            self.userSignNameBlock(self.textView.text);
        }
    }
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    [self.textField becomeFirstResponder];
    if (self.userSignName) {
        [self.textView becomeFirstResponder];
    }
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.view endEditing:YES];

}

- (void)setUpUserNameUI {
    
    FUITextField *textField = [[FUITextField alloc] initWithFrame:CGRectMake(0, kAutoHei(24), kYFScreenWidth, kAutoHei(44))];
    self.textField = textField;
    [self.view addSubview:textField];
    textField.textColor = [UIColor colorWithHex:0x787878];
    textField.font = [UIFont systemFontOfSize:16];
    textField.edgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
    
    textField.backgroundColor = [UIColor whiteColor];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:self.textField];
    
    textField.delegate = self;
    
    self.textField.text = self.userName;
    
    //    添加分隔线
    UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0,kAutoHei(24) - 0.5, kYFScreenWidth, 0.5)];
    topLine.backgroundColor = kLineBackgroundColor;
    [self.view addSubview:topLine];
    
    UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(textField.frame), kYFScreenWidth, 0.5)];
    bottomLine.backgroundColor = kLineBackgroundColor;
    [self.view addSubview:bottomLine];
}




/**
 *  初始化签名的输入框
 */
- (void)setUpUserSignNameUI {
    
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, kAutoHei(24), kYFScreenWidth, kAutoHei(64))];
    
    textView.textColor = [UIColor colorWithHex:0x787878];
    textView.font = [UIFont systemFontOfSize:16];
    textView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:textView];
    
    CGFloat labelW = 50;
    CGFloat labelH = 30;
    self.textView = textView;
    textView.delegate = self;
    
    UILabel *countLabel = [[UILabel alloc] initWithFrame:CGRectMake((kYFScreenWidth - labelW - 16), kAutoHei(88) - labelH, labelW, labelH)];
    
    countLabel.textColor = [UIColor colorWithHex:0xaaaaaa];
    countLabel.font = [UIFont systemFontOfSize:14];
    countLabel.textAlignment = NSTextAlignmentRight;
    
    [self.view addSubview:countLabel];
    self.countLable = countLabel;
    
    if (self.userSignName.length > 15) {
        self.textView.text = [self.userSignName substringToIndex:15];
    }else {
        self.textView.text = self.userSignName;
    }
    
    self.countLable.text = [NSString stringWithFormat:@"%zd", (kUserSignNameMaxNum - self.textView.text.length)];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidValueChange:) name:UITextViewTextDidChangeNotification object:nil];
    
    self.textView.textContainerInset = UIEdgeInsetsMake(6, 15, 6, 15);
 
    UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0,kAutoHei(24) - 0.5, kYFScreenWidth, 0.5)];
    topLine.backgroundColor = kLineBackgroundColor;
    [self.view addSubview:topLine];
    
    UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(textView.frame), kYFScreenWidth, 0.5)];
    bottomLine.backgroundColor = kLineBackgroundColor;
    [self.view addSubview:bottomLine];
    
}

- (void)textViewDidValueChange:(NSNotification *)note {
    
    if (self.textView.text.length > kUserSignNameMaxNum) {
         self.textView.text = [self.textView.text substringToIndex:kUserSignNameMaxNum];
    }
    self.countLable.text = [NSString stringWithFormat:@"%zd", (kUserSignNameMaxNum - self.textView.text.length)];
    
}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSString *newText = [textView.text stringByReplacingCharactersInRange:range withString:text];
    if (newText.length > kUserSignNameMaxNum) {
        return NO;
    }
    return YES;
}

- (void)textFieldDidChange:(NSNotification *)note {
    if ([NSBundle currentLanguageIsChinese]) {
        if (self.textField.text.length > 15)
        {
            self.textField.text = [self.textField.text substringToIndex:15];
        }
    }
    
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if ([NSBundle currentLanguageIsChinese]) {
        // 名字不能超过15个字
        NSInteger kUserNameLnegth = 15;
        NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if (newText.length > kUserNameLnegth) {
            return NO;
        }
    }
    
    return YES;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
